tree grammar VisEvaluatorWalker;

options {
  language = Java;
  tokenVocab = SampleAST;
  ASTLabelType = CommonTree;
}

@header{ 
 package ast.gen;
 import java.util.Map;
 import java.util.HashMap;
}

@members {
  private Map<String, String> variables = new HashMap<String, String>();
}


evaluator returns [String result]
  : assigment* e=expression EOF { result = e; }
  ;

assigment 
  : ^(':=' IDENT e=expression)
    {
      variables.put($IDENT.text,e);
    }
  ;
expression returns [String result]
  : ^('+' op1=expression op2=expression) { result = new String(" +(" + op1 + " " + op2 + ")");}
  | ^('-' op1=expression op2=expression) { result = new String(" -(" + op1 + " " + op2 + ")");}
  | ^('*' op1=expression op2=expression) { result = new String(" *(" + op1 + " " + op2 + ")");}
  | ^('/' op1=expression op2=expression) { result = new String(" /(" + op1 + " " + op2 + ")");}
  | ^('%' op1=expression op2=expression) { result = new String(" \%(" + op1 + " " + op2 + ")");}
  | ^(NEGATION e=expression) { result = e; }
  | IDENT { 
    
//    result = variables.get();
    if(variables.containsKey($IDENT.text)){
      result = variables.get($IDENT.text);
    }else{
      System.out.println($IDENT.text + " :init to 0");
      result = "0";
    }
   }
  | INTEGER {result = new String( "" + $INTEGER.text);}
  ;
  